package com.moon.game.manager;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.log4j.Logger;

import com.moon.game.resource.model.Notice;
import com.moon.game.util.GDateUtils;

/**
 * @description 公告管理
 * @author Ranger 作者 E-mail:haozi000005@163.com
 * @version 创建时间：2012-7-25 下午3:17:47
 */
public class NoticeMG {
	private static final Logger log = Logger.getLogger(MonsterMG.class);
	public static ConcurrentHashMap<Integer, Notice> notices = new ConcurrentHashMap<Integer, Notice>();
	
	/**
	 * 获取所有公告
	 * 
	 * @return
	 */
	public final static List<Notice> getNotices() {
		List<Notice> noticeList = new ArrayList<Notice>();
		for (Map.Entry<Integer, Notice> entry : notices.entrySet()) {
			noticeList.add(entry.getValue());
		}
		return noticeList;
	}
	
	/**
	 * 查看指定id的公告
	 * 
	 * @param id
	 * @return
	 */
	public final static Notice getTheNotice(int id) {
		return notices.get(id);
	}
	private NoticeMG() {
	}
	public static NoticeMG instance = new NoticeMG();
	public void init() {
		log.debug("load notices start...");
		loadNotices();
		log.debug("load notices end...");
	}
	private void loadNotices() {
		List<Notice> noticeList = DBManager.queryAllNotices();
		if (noticeList != null) {
			for (Notice notice : noticeList) {
				if (notice.getStartShowTime() != null && notice.getEndShowTime() != null) {
					if (notice.getStartShowTime().getTime() >= GDateUtils.getMillisNow()
							&& notice.getEndShowTime().getTime() <= GDateUtils.getMillisNow()) {
						notices.put(notice.getId(), notice);
					}
				}else {
					notices.put(notice.getId(), notice);
				}
			}
		}
	}
}
